package cn.lingyangwl.agile.msg.push.controller;

import cn.lingyangwl.agile.msg.push.model.rqrs.field.*;
import cn.lingyangwl.agile.msg.push.service.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.validation.group.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 消息字段库
 * 
 * @author shenguangyang
 */
@Api(tags = "消息字段库")
@RestController
@RequestMapping("/msgFieldLibrary")
public class FieldLibraryController {
    @Resource
    private FieldLibraryService fieldLibraryService;

    @ApiOperation(value = "查询消息字段库")
    @GetMapping("/list")
    @RamResource(action = "msg:fieldLibrary:query", name = "查询消息字段库", type = ActionType.READ)
    public Resp<List<FieldLibraryResp>> listFieldLibrary(@RequestParam(required = false) String keyWord) {
        return Resp.ok(fieldLibraryService.listField(keyWord));
    }

    @ApiOperation(value = "保存消息字段库")
    @PostMapping("/save")
    @RamResource(action = "msg:fieldLibrary:save", name = "保存消息字段库", type = ActionType.WRITE)
    public Resp<?> saveFieldLibrary(@Validated(InsertGroup.class) @RequestBody FieldLibrarySaveReq req) {
        return Resp.ok(fieldLibraryService.saveField(req));
    }
  
    @ApiOperation(value = "更新消息字段库")
    @PostMapping("/update")
    @RamResource(action = "msg:fieldLibrary:update", name = "更新消息字段库", type = ActionType.WRITE)
    public Resp<?> updateFieldLibrary(@Validated(UpdateGroup.class) @RequestBody FieldLibrarySaveReq req) {
        return Resp.ok(fieldLibraryService.updateField(req));
    }
  
    @ApiOperation(value = "批量删除消息字段库")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "msg:fieldLibrary:delete", name = "删除消息字段库", type = ActionType.WRITE)
    public Resp<?> batchDeleteFieldLibrary(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(fieldLibraryService.batchDeleteField(ids));
    }
  
    @ApiOperation(value = "删除消息字段库")
    @DeleteMapping("/delete")
    @RamResource(action = "msg:fieldLibrary:delete", name = "删除消息字段库", type = ActionType.WRITE)
    public Resp<?> deleteFieldLibrary(@RequestParam(value = "id") Long id) {
        return Resp.ok(fieldLibraryService.batchDeleteField(Collections.singletonList(id)));
    }
}
